/**
 * 详情对话框
 */
var GoodsSkuInfoDlg = {
    data: {
        id: "",
        goodsId: "",
        skuName: "",
        skuDesc: "",
        price: "",
        cost: "",
        stock: "",
        weight: "",
        skuPic: "",
    }
};



layui.use(['form', 'admin', 'ax','upload','element','simTool'], function ()   {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
    var element = layui.element;
    var simtool = layui.simTool;



    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/goodsSku/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();


    //页面渲染
    form.val('goodsSkuForm', result.data);
    $("#editSkuPicBox").find("img").attr("src",Feng.ctxPath+result.data.skuPic)

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        form.render()
        var ajax = new $ax(Feng.ctxPath + "/goodsSku/editItem", function (data) {
            Feng.success("修改成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
    });


    //让当前iframe弹层高度适应
    //admin.iframeAuto();
    var sim = new simtool("/upload","#editSkuPicBox");
    sim.uploadImg()

    /**
     * 点击修改规格数据
     */
    GoodsSkuInfoDlg.updateSku=function (event) {
        var parent2 = $(event.target).parent().parent();
        //数据收集放进data
        GoodsSkuInfoDlg.innerData(parent2)
        var ajax = new $ax(Feng.ctxPath + "/goodsSku/editItem", function (data) {
            Feng.success("更新成功!");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.setData(GoodsSkuInfoDlg.data);
        ajax.start();
    }


    /**
     * 用于获取特定区域的页面变量
     */
    GoodsSkuInfoDlg.setData=function(obj,key,value){
        var info={}
        GoodsSkuInfoDlg.data[key] = obj.find("#" + key).val()!= "undefined" ? obj.find("#" + key).val(): obj.find('input[name="key"]').val();
    }

    /**
     * 插入规格数据
     * @param obj
    */
    GoodsSkuInfoDlg.innerData=function(obj){
        GoodsSkuInfoDlg.setData(obj,"goodsId");
        GoodsSkuInfoDlg.setData(obj,"skuName");
        GoodsSkuInfoDlg.setData(obj,"weight");
        GoodsSkuInfoDlg.setData(obj,"cost");
        GoodsSkuInfoDlg.setData(obj,"price");
        GoodsSkuInfoDlg.setData(obj,"skuDesc");
        GoodsSkuInfoDlg.setData(obj,"stock");
        GoodsSkuInfoDlg.setData(obj,"skuPic");
        GoodsSkuInfoDlg.setData(obj,"id");
    }

    /**
     * 点击加号显示表单
     */
    $("#addSkuBtn").click(function () {
        $("#goodsSkuBox").css("opacity",1);
        $("#collapse-box").css("opacity",0);
        $("#goodsSkuBox").css("z-index",1000);
        $("#collapse-box").css("z-index",100);
    })

    /**
     * 废弃sku
     */
    GoodsSkuInfoDlg.deleteSku=function () {
        var skuId = $(this).parent().parent().find("#id").val();
        if(skuId=="" ){
            var parent = $(this).parent().parent().parent().parent();
            $(".layui-collapse").remove(parent);
        }else {
            Feng.info("逻辑删除这个规格")
        }

    }

    /**
     * 确定添加
     */
    /*var app = new Vue({

        el: '#goodsSkuBox',
        data: {
            goodsId: '!',
            cost: '',
            skuName: '',
            sotck: '',
            weight: '',
            price: '',
            skuDesc:"",
        }
    })*/
});